﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HTTT.Utilities;
using HTTT.Bussiness;
using HTTT.DataAccess;
using System.Web.Security;

namespace HTTT.admin.htuser
{
	public partial class UserList : System.Web.UI.Page
	{
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["keyword"] != null)
                {
                    txtKeyword.Text = Session["keyword"].ToString();
                }
                CheckPermission();
                LoadData();
            }
        }

        private void CheckPermission()
        {
            if (Page.User.IsInRole(Constants.ROLE_SUPER_ADMINISTRATOR))
            {
                //
            }
            else
            {
                Response.Redirect("/admin/AccessDenied.aspx");
            }
        }

        private void LoadData()
        {
            MembershipUserCollection Data = Membership.GetAllUsers();
            List<MembershipUser> BindData = new List<MembershipUser>();
            if (Data == null) return;
            string keyword = txtKeyword.Text.Trim();

            foreach (MembershipUser m in Data)
            {
                if (keyword != string.Empty)
                {
                    if (m.UserName.Contains(keyword) || m.Email.Contains(keyword))
                    {
                        BindData.Add(m);
                    }
                }
                else
                {
                    BindData.Add(m);
                }
            }
            
            if(BindData != null)
            {
                rptData.DataSource = BindData;
                rptData.DataBind();
            }
        }
        protected void btnSearch_Click(Object sender, EventArgs e)
        {
            Session["keyword"] = txtKeyword.Text.Trim();
            LoadData();
        }

        protected void btnClear_Click(Object sender, EventArgs e)
        {
            txtKeyword.Text = "";
            Session["keyword"] = txtKeyword.Text.Trim();
            LoadData();
        }
        protected void btnAdd_Click(Object sender, EventArgs e)
        {
            Response.Redirect("AddUser.aspx");
        }

        protected void btnEdit_Click(Object sender, EventArgs e)
        {
            string username = GetSelectedID();
            if (string.IsNullOrEmpty(username))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Message", Common.FormatAlertMsg("Phải chọn 1 User"), true);
                return;
            }
            Response.Redirect("ResetPwd.aspx?ID=" + username);
        }
        protected void btnDelete_Click(Object sender, EventArgs e)
        {
            string username = GetSelectedID();
            if (string.IsNullOrEmpty(username))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Message", Common.FormatAlertMsg("Phải chọn 1 User"), true);
                return;
            }
            if (ID != null)
            {
                Membership.DeleteUser(username);
            }
            Response.Redirect("UserList.aspx");
        }
        
        private string GetSelectedID()
        {
            string username ="";
            foreach (RepeaterItem ri in rptData.Items)
            {
                CheckBox chk = (CheckBox)ri.FindControl("chkItem");
                if (chk.Checked)
                {
                    username = chk.Attributes["value"];
                }
            }
            return username;
        }
	}
}